package ca.etsmtl.log430.lab3.donnee.txtreader;

import java.util.ArrayList;
import java.util.Collection;

import ca.etsmtl.log430.lab3.donnee.beans.CourseBean;
import ca.etsmtl.log430.lab3.donnee.beans.CourseBeanBuilder;
import ca.etsmtl.log430.lab3.donnee.ifaces.CourseReader;
/**
 * @author  samuel
 */
public class FileCourseReader extends LineOfTextFileReader implements CourseReader {

	/**
	 * List of courses.
	 * @uml.property  name="listOfCourses" multiplicity="(0 -1)"
	 */
    private Collection<CourseBean> listOfCourses = new ArrayList<CourseBean>();

    public FileCourseReader() {

        setListOfCourses(null);

    } // Constructor #1

    public FileCourseReader(String InputFile) {

    	this.listOfCourses = getCourseList(InputFile);
       // setListOfCourses();

    } // Constructor #2

	/**
	 * Reads a line of text from the course file. The line of text is then
	 * passed to the ParseCourseText method where it is parsed and a Course
	 * object is returned. The Course object is then added to the list. When a
	 * null is read from the Course file the method terminates and returns the
	 * list to the caller. A list that points to null is an empty list.
	 * 
	 * @param inputFile
	 * @return The CourseList properly populated
	 */
    public Collection<CourseBean> getCourseList(String inputFile) {

        String courseText;
        boolean done;
        ArrayList<CourseBean> list = new ArrayList<CourseBean> ();

        if (openFile(inputFile)) {

            done = false;

            while (!done) {

                try {

                    courseText = readLineOfText();

                    if (courseText == null) {

                        done = true;

                    } else {

                        list.add(parseCourseText(courseText));

                    } // if 

                } // try

                catch (Exception Error) {

                    return (null);

                } // catch

            } // while		

        } else {

            return (null);

        } // if

        closeFile();

        return (list);

    } // GetCourseList

	/**
	 * Parse lines of text that are read from the text file containing course
	 * information in the above format. Note that this is a private method and
	 * it is used exclusively by getCourseList.
	 * 
	 * @param lineOfText
	 * @return A properly populated Course instance.
	 */
    private CourseBean parseCourseText(String lineOfText) {

        boolean done; // Loop terminator
        String token; // String token parsed from LineOfText
        int tokenCount; // Number of tokens parsed
        int frontIndex; // Front index of token to parse
        int backIndex; // Back index of token to parse

        // Instantiate a course object

    
        CourseBeanBuilder builder= new CourseBeanBuilder();
        tokenCount = 0;
        frontIndex = 0;
        backIndex = 0;
        done = false;

        while (!done) {

            backIndex = lineOfText.indexOf(' ', frontIndex);

            if (tokenCount < 6) {

                token = lineOfText.substring(frontIndex, backIndex);

            } else {

                token = lineOfText.substring(frontIndex);

            } // if 

            switch (tokenCount) {

                case 0 : // Course number 

                	builder.setCourseID(token);
                    frontIndex = backIndex + 1;
                    tokenCount++;
                    break;

                case 1 : // Course group
                	builder.setGroup(token);
                    frontIndex = backIndex + 1;
                    tokenCount++;
                    break;

                case 2 : // Course Type
                	builder.setType(token);
                    frontIndex = backIndex + 1;
                    tokenCount++;
                    break;

                case 3 : // Day of the week the course will be held on
                	builder.setDay(token);
                    frontIndex = backIndex + 1;
                    tokenCount++;
                    break;

                case 4 : // Course start time
                	builder.setStartTime(token);
                    frontIndex = backIndex + 1;
                    tokenCount++;
                    break;

                case 5 : // Course stop time
                	builder.setStopTime(token);
                    frontIndex = backIndex + 1;
                    tokenCount++;
                    break;

                case 6 : // Course title or name of the course
                	builder.setCourseName(token);
                    done = true;
                    break;

            } // end switch

        } // end while

        return (builder.build());

    } // ParseCourseText

	public void setListOfCourses(ArrayList<CourseBean> listOfCourses) {
		this.listOfCourses = listOfCourses;
	}

	/**
	 * @return
	 * @uml.property  name="listOfCourses"
	 */
	@Override
	public Collection<CourseBean> getListOfCourses() {
		return listOfCourses;
	}



} // CourseReader